package com.yueya.basic.news.subject.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.news.subject.model.Subject;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;

public class SubjectController extends BaseController {

	Map<String, Object> map = new HashMap<String, Object>();

	public void index() {
		render("/WEB-INF/views/basic/news/subject/list.jsp");
	}

	public void selectDept() {
		render("/WEB-INF/views/basic/news/subject/select.jsp");
	}

	/**
	 * easyui tree
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void listTree() {
		List list = new ArrayList();
		list.add(Subject.service.recursiveTree("000"));
		String json = JsonKit.toJson(list);
		renderJson(json);

	}

	public void getDatagrid() {
		map.clear();
		map.put("level", 1);
		Record d = null;
		if (getPara("parentid") != null) {
			map.put("parentid", getPara("parentid"));
			d = Db.findFirst("select * from t_subject where subject_id = '" + getPara("parentid") + "'");
		} else {
			map.put("order_name", "order_id");
			map.put("order", "asc");
		}

		if (getPara("name") != null) {
			map.put("name", getPara("name"));
		}
		Page<Record> page = Subject.service.pagination(getPage(), getRows(), map);
		List<Record> list = page.getList();
		if (d != null) {
			list.add(d);
		}

		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void doAdd() {
		String pid = this.getPara("pid");
		Subject subject = new Subject();
		subject.set("parent_id", pid);
		this.setAttr("subject", subject);
		render("/WEB-INF/views/basic/news/subject/edit.jsp");
	}

	public void doEdit() {
		String id = this.getPara("id");
		Subject subject = Subject.dao.findById(id);
		this.setAttr("subject", subject);
		render("/WEB-INF/views/basic/news/subject/edit.jsp");
	}

	public void save() {
		Subject subject = this.getModel(Subject.class);
		boolean bl = Subject.service.save(subject);
		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("操作成功.");
		} else {
			j.setSuccess(false);
			j.setMsg("操作失败.");
		}
		this.renderJson(j);
	}

	public void doRemove() {
		String id = this.getPara("id");
		Subject subject = Subject.dao.findById(id);
		subject.set("del_flag", "1");
		boolean bl = subject.update();
		if (bl == true) {
			renderHtml("success");
		} else {
			renderHtml("fail");
		}
	}

	/**
	 * 全部删除
	 */
	public void doRemoves() {
		Boolean bl = false;
		String idsStr = this.getPara("ids");
		if (idsStr != null) {
			String[] ids = idsStr.split(";");
			Subject subject = null;
			for (int i = 0; i < ids.length; i++) {
				if (!"".equals(ids[i]) && ids[i] != null) {
					subject = Subject.dao.findById(ids[i]);
					subject.set("del_flag", "1");
					if (subject.update()) {
						bl = true;
					} else {
						bl = false;
						break;
					}
				}

			}
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

  
}
